package me.angeldevil.autoscrollviewpager.demo;

import me.angeldevil.autoscrollviewpager.AutoScrollViewPager;
import me.angeldevil.autoscrollviewpager.demo.slice.ViewPagerIndicator;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

public class AutoScrollPagerFragment extends BaseFraction {
    private String[] titles = {"Page 1", "Page 2", "Page 3"};
    private int[] imgs = {ResourceTable.Media_bg_ios, ResourceTable.Media_bg_js, ResourceTable.Media_bg_other};
    private ToastDialog toast;
    private Context context;
    private DependentLayout toastView;
    private Text toastText;

    public AutoScrollPagerFragment(Context context) {
        this.context = context;
    }

    @Override
    protected void initComponent(Component component) {
        AutoScrollViewPager pager = (AutoScrollViewPager) component.findComponentById(ResourceTable.Id_scroll_pager);
        final Text title = (Text) component.findComponentById(ResourceTable.Id_title);

        ViewPagerIndicator indicator = (ViewPagerIndicator) component.findComponentById(ResourceTable.Id_indicator);

        pager.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int i) {
                title.setText(titles[i]);
            }
        });

        pager.setProvider(new PageSliderProvider() {
            @Override
            public int getCount() {
                return imgs.length;
            }

            @Override
            public Object createPageInContainer(ComponentContainer componentContainer, int position) {
                Image view = new Image(componentContainer.getContext());
                ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig();
                config.width = ComponentContainer.LayoutConfig.MATCH_PARENT;
                config.height = ComponentContainer.LayoutConfig.MATCH_PARENT;
                view.setLayoutConfig(config);
                view.setScaleMode(Image.ScaleMode.CLIP_CENTER);
                view.setPixelMap(imgs[position]);
                componentContainer.addComponent(view);
                return view;
            }

            @Override
            public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
                componentContainer.removeComponent((Component) o);
            }

            @Override
            public boolean isPageMatchToObject(Component component, Object o) {
                return component == o;
            }
        });
        pager.setScrollFactor(5);
        pager.setPageCacheSize(4);
        pager.startAutoScroll(2000);
        pager.setOnPageClickListener(new AutoScrollViewPager.OnPageClickListener() {

            @Override
            public void onPageClick(AutoScrollViewPager autoScrollPager, int position) {
                if (toast != null) {
                    toast.cancel();
                    toast = null;
                }
                toast = new ToastDialog(context);
                toastView = (DependentLayout) LayoutScatter.getInstance(getFractionAbility()).parse(ResourceTable.Layout_faction_toast, null, false);
                toastText = (Text) toastView.findComponentById(ResourceTable.Id_text);
                toastText.setText("You clicked page: " + (position + 1));
                toast.setComponent(toastView);
                toast.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
                toast.setTransparent(true);
                toast.setOffset(0, 100);
                toast.show();
            }
        });
        indicator.setOnIndicatorClickListener(new ViewPagerIndicator.OnIndicatorClickListener() {
            @Override
            public void onClik(int pos) {
                pager.setCurrentPage(pos);
            }
        });
        indicator.setViewPager(pager);
    }

    @Override
    protected int getUIContent() {
        return ResourceTable.Layout_fraction_auto_scroll_pager;
    }

}
